//
//  Person.m
//  semple-code-oc
//
//  Created by zhaoguoteng on 2022/1/5.
//

#import "Person.h"
#import <objc/runtime.h>

@implementation Person

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        Class cls = [self class];
        
        SEL sel1 = @selector(run);
        SEL sel2 = @selector(hook_run);
        
        Method m1 = class_getInstanceMethod(cls, sel1);
        Method m2 = class_getInstanceMethod(cls, sel2);
        
        IMP imp1 = class_getMethodImplementation(cls, sel1);
        IMP imp2 = class_getMethodImplementation(cls, sel2);
            
        BOOL canAddMethod = class_addMethod(cls, sel1, imp2, method_getTypeEncoding(m2));
        if (canAddMethod) {
            class_replaceMethod(cls, sel2, imp1, method_getTypeEncoding(m1));
        } else {
            method_exchangeImplementations(m1, m2);
        }
    });
}

//- (void)run {
//
//}

- (void)hook_run {
    [self hook_run];
    
    DDLogDebug(@"hook person run");
}

@end
